package com.lisx.module_add_new_word.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.lisx.module_add_new_word.R;
import com.lisx.module_add_new_word.adapter.GkAdapter;
import com.lisx.module_add_new_word.bean.MyDatabase;
import com.lisx.module_add_new_word.bean.WordBean;
import com.lisx.module_add_new_word.databinding.ActivityGuanqiaBinding;
import com.lisx.module_add_new_word.utils.QueryUtils;
import com.lisx.module_add_new_word.utils.SpHelper;

import java.util.ArrayList;
import java.util.List;

public class GuanqiaActivity extends AppCompatActivity {
    private ActivityGuanqiaBinding binding;
    private RecyclerView recyclerView;
    private GkAdapter adapter;
    private List<WordBean> wordBeans = new ArrayList<>();

    public static void start(Context context){
        Intent intent = new Intent(context, GuanqiaActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuanqiaBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();

    }

    private void initData() {
        boolean isFirst = SpHelper.getFirstTime(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                wordBeans.clear();
                if (isFirst){
                    //第一次加载 从json数据文件获取
                    SpHelper.setFirstTime(GuanqiaActivity.this,false);
                    wordBeans = QueryUtils.queryWord(GuanqiaActivity.this);
                }else {
                    //以后的加载都是从数据库里获取
                   wordBeans = MyDatabase.getInstance(GuanqiaActivity.this).wordBeanDao().getWordList();
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(GuanqiaActivity.this,5);
                        adapter = new GkAdapter(GuanqiaActivity.this,wordBeans);
                        binding.wordList.setLayoutManager(gridLayoutManager);
                        binding.wordList.setAdapter(adapter);
                        adapter.setOnItemClickLitener(new GkAdapter.OnItemClickLitener() {
                            @Override
                            public void onItemClick(int position) {
                                int select = wordBeans.get(position).getSelect();
                                if (select == 0){
                                    ToastUtils.showShort("先做对前面的题目!");
                                    return;
                                }
                                startActivity(new Intent(GuanqiaActivity.this,AddNewWordActivity.class).putExtra("position",position).putExtra("zi",wordBeans.get(position).getZi_content()));
                            }
                        });
                    }
                });
            }
        }).start();
    }

    private void initView() {
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }


}
